/**
 * Session package
 * 
 * This package will contain user access, local storage, and handle 
 * persistence on the client side of the SNApplication. 
 */

snapp.session = {
               
	// Placeholder for session access
	access: null,
	
	// create login div (like it says)
	createLoginDiv: function() {
		this.loginButton = new Element('div',{
			'class': 'loginbutton'
		});
		this.loginButton.store('togglestate','IN');
		this.toggleLoginDiv();
		this.loginButton.inject(document.body);
	},

	toggleLoginDiv: function() {
		tstate = this.loginButton.retrieve('togglestate');
		label = tstate == 'IN' ? 'Sign In' : 'Sign Out';
		funct = tstate == 'IN'
			?	function() {
					this.access = new SNAppAccess();
					this.popLogin();
				} 
			:	function() {
					this.logOut();
				};
		bound = funct.bind(this);
		this.loginButton.removeEvents('click');
		this.loginButton.addEvent('click', bound);
		this.loginButton.set('text', label);
		
	},
	
	mask: null,
	logindiv: null,
	popLogin: function() {
		// build mask
		this.mask = new Mask();
		this.mask.show();
		//this.mask.addEvent('click',this.dropLogin.bind(this));
		// get position based on viewport size
		tp = (document.body.getSize().y/2)-150;
		lft = (document.body.getSize().x/2)-200;
		// build div
		this.logindiv = new Element('div',{'id':'login_wrapper','styles':{top:tp,left:lft}}).inject(document.body);
		// fetch login form
		this.logindiv.set('load',{'onSuccess': function() {$('loginuid').focus();}});
		this.logindiv.load('content/forms/access/login.jsp');
	},
	
	dropLogin: function() {
		this.waiter.stop();
		this.logindiv.release();
		this.logindiv.destroy();
		this.mask.destroy();

		return false;
	},
	
	handleLogin: function(accObj) {
		if (accObj.getResultCode() > 0) {
			// set validity
			accObj.isValid = parseInt(accObj.getProperty('id')) > 0;
			
			if (accObj.isValid) {
				
				this.dropLogin();	
				
				myos.windows.closeAll();
				
				this.validate(accObj);
			}
			
		} else {
			alert('Invalid Credentials');
			this.dropLogin();
		}
	},

	checkSession: function() {
		this.access = new SNAppAccess();
		this.access.checkSession();
	},
	
	handleSessionCheck: function(accObj) {
		if (accObj.getResultCode() > 0) {
			// set validity
			accObj.isValid = parseInt(accObj.getProperty('id')) > 0;
			
			if (accObj.isValid) {
				
				myos.windows.closeAll();

				this.validate(accObj);
			}
			
		}
	},
	
	validate: function(accObj) {
		this.access = accObj;
		functs = accObj.getProperty('functions');

		layout.menus.main.build(functs);
		
		this.loginButton.store('togglestate','OUT');
		this.toggleLoginDiv();

	},
	
	logOut: function() {
		req = new Request({
			'url': snapp.getURL('snapp_util')+'?action=access.Logout',
			'method': 'post',
			onComplete: function() {
				myos.windows.closeAll();
				layout.menus.main.load();
				snapp.session.loginButton.store('togglestate','IN');
				snapp.session.toggleLoginDiv();
				delete snapp.session.access;
			}
		});
		req.send();
	}
};

